package net.chromedpork.hypertext.validation;


/**
 *
 *  This class provides a set of static methods that are intended
 *  to be used for the assertion of sanity upon input data.
 *
 *  @author Inode <inode@chromedpork.net>
 *
 */

public class Assertion
{
    /**
     *
     *  Tests if a candidate string does not match a regular expression.
     *
     *  @param candidate
     *  @param expression
     *  @param msg
     *
     *  @throws AssertionError if candidate does not match expression
     *
     */

    public static void match(String candidate, String expression, String msg)
                       throws AssertionError
    {
        notNull(candidate, "matches(): candidate string is null");
        notNull(expression, "matches(): expression is null");
        notNull(msg, "matches(): message is null");

        if (candidate.matches(expression) == false)
        {
            throw new AssertionError(msg);
        }
    }


    /**
     *
     *  Tests if a candidate string matches a regular expression.
     *
     *  @param candidate
     *  @param expression
     *  @param msg
     *
     *  @throws AssertionError if candidate matches expression
     *
     */

    public static void dontMatch(String candidate, String expression, String msg)
                       throws AssertionError
    {
        notNull(candidate, "matches(): candidate string is null");
        notNull(expression, "matches(): expression is null");
        notNull(msg, "matches(): message is null");

        if (candidate.matches(expression) == true)
        {
            throw new AssertionError(msg);
        }
    }


    /**
     * 
     *  Tests if an object is null.
     *
     *  @param obj      an arbitrary object
     *  @param msg      a message associated with the condition
     *
     *  @throws AssertionError if object is null.
     *
     */

    public static void notNull(Object obj, String msg)
                       throws AssertionError
    {
        if (obj == null)
        {
            throw new AssertionError(msg);
        }
    }


    /**
     *
     *  Tests if an integer is of negative value.
     *
     *  @param obj      an arbitrary integer
     *  @param msg      a message associated with the condition
     *
     *  @throws AssertionError if obj value is negative.
     *
     */

    public static void notNegative(int obj, String msg)
                       throws AssertionError
    {
        notNull(msg, "notNegative(): message is null");

        if (obj < 0)
        {
            throw new AssertionError(msg);
        }
    }


    /**
     *
     *  Tests if an integer is less than or equal, to zero,
     *
     *  @param obj      an arbitrary integer
     *  @param msg      a message associated with the condition
     *
     *  @throws AssertionError if obj value is negative or zero.
     *
     */

    public static void notNegativeNorZero(int obj, String msg)
                       throws AssertionError
    {
        notNull(msg, "notNegativeNorZero(): message is null");
        
        if (obj <= 0)
        {
            throw new AssertionError(msg);
        }
    }


    /**
     *
     *  Tests if an integer value is outside of the range defined
     *  by the minimum and maximum value boundaries.
     *
     *  @param obj      an arbitrary integer
     *  @param min      a minimum value boundary
     *  @param max      a maxmimum value boundary
     *  @param msg      a message associated with the condition
     *
     *  @throws AssertionError if obj value is zero.
     *
     */

    public static void notOutOfBounds(int obj, int min, int max, String msg)
                       throws AssertionError
    {
        notNull(msg, "notOutOfBounds(): message is null");
        
        if ((obj < min) || (obj > max))
        {
            throw new AssertionError(msg);
        }
    }


    /**
     *
     *  Tests if an integer is zero,
     *
     *  @param obj      an arbitrary integer
     *  @param msg      a message associated with the condition
     *
     *  @throws AssertionError if obj value is zero.
     *
     */

    public static void notZero(int obj, String msg)
                       throws AssertionError
    {
        notNull(msg, "notZero(): message is null");
        
        if (obj == 0)
        {
            throw new AssertionError(msg);
        }
    }
}
